package org.csu.small.controller.front;


import org.csu.small.common.Constant;
import org.csu.small.common.ResponseCode;
import org.csu.small.common.UniformResponse;
import org.csu.small.entity.User;
import org.csu.small.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


@RequestMapping("/cart/")
@Controller
public class CartController {
    @Autowired
    private ICartService cartService;

    /**
     * 添加产品
     * @param productId
     * @param count
     * @param session
     * @return
     */
    @PostMapping("addCart")
    @ResponseBody
    public UniformResponse addCart(Integer productId, Integer count, HttpSession session) {
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return cartService.add(user.getId(), productId, count);
    }

    /**
     * 更新数量
     * @param productId
     * @param count
     * @param session
     * @return
     */
    @PostMapping("updateCartNum")
    @ResponseBody
    public UniformResponse updateCartNum(Integer productId,Integer count,HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return cartService.updateCartNum(user.getId(), productId, count);
    }

    /**
     * 移除产品
     * @param productIds
     * @param session
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public UniformResponse delete(String productIds,HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return cartService.deleteByProductIds(user.getId(), productIds);
    }

    /**
     * 购物车列表
     * @param session
     * @return
     */
    @GetMapping("cartList")
    @ResponseBody
    public UniformResponse getCartList(HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return cartService.getCartList(user.getId());
    }

    /**
     * 选择购物车商品
     * @param productId
     * @param session
     * @return
     */
    @PostMapping("select")
    @ResponseBody
    public UniformResponse select(Integer productId,HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return cartService.selectOrUnSelect(user.getId(),productId,Constant.Cart.CHECKED);
    }

    /**
     * 取消选择
     * @param productId
     * @param session
     * @return
     */
    @PostMapping("unSelect")
    @ResponseBody
    public UniformResponse unSelect(Integer productId,HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return cartService.selectOrUnSelect(user.getId(),productId,Constant.Cart.UN_CHECKED);
    }

    /**
     * 全选
     * @param session
     * @return
     */
    @PostMapping("selectAll")
    @ResponseBody
    public UniformResponse selectAll(HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return cartService.selectOrUnSelectAll(user.getId(),Constant.Cart.CHECKED);
    }


    /**
     * 取消全选
     * @param session
     * @return
     */
    @PostMapping("unSelectAll")
    @ResponseBody
    public UniformResponse unSelectAll(HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return cartService.selectOrUnSelectAll(user.getId(),Constant.Cart.UN_CHECKED);
    }


    /**
     * 获取购物陈商品数量
     * @param session
     * @return
     */
    @PostMapping("getCartProductCount")
    @ResponseBody
    public UniformResponse<Integer> getCartProductCount(HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDescription());
        }
        return cartService.getCartProductCount(user.getId());
    }

}
